{extend name="../../admin/view/main"}

{block name="content"}
<form action="{:sysuri()}" method="post" data-auto="true" class="layui-form layui-card" id="GoodsForm">
    <div class="layui-card-body padding-40 padding-bottom-20">

        <div class="layui-col-xs4">
                


            {notempty name='cateorys'}
            <div class="layui-form-item relative block">
                <span class="color-green font-w7 label-required-prev">种类</span>
                <select name="category" class="layui-select" required lay-search>
                    {foreach $cateorys as $k=>$v}
                    {if isset($vo.category) and $vo.category eq $v.code}
                    <option selected value="{$v.code}">{$v.name}</option>
                    {else}
                    <option value="{$v.code}">{$v.name}</option>
                    {/if}
                    {/foreach}
                </select>
            </div>
            {/notempty}

            
            <!-- <label class="layui-form-item relative block " >
                <span class="color-green font-w7">框数</span>
                <input class="layui-input" name="box_num" type="number" placeholder="请输入" value='{$vo.box_num|default=""}'>
            </label> -->
            

            {notempty name='warehouses'}
            <div class="layui-form-item relative block">
                <span class="color-green font-w7 label-required-prev">客户</span>
                <select name="depart_code" class="layui-select" required lay-search>
                    <option value="">请选择</option>
                    {foreach $warehouses as $k=>$v}
                    {if isset($vo.depart_code) and $vo.depart_code eq $v.ware_code}
                    <option selected value="{$v.ware_code}">{$v.ware_name}</option>
                    {else}
                    <option  value="{$v.ware_code}">{$v.ware_name}</option>
                    {/if}
                    {/foreach}
                </select>
            </div>
            {/notempty}

          

            {notempty name='warehouses'}
            <div class="layui-form-item relative block">
                <span class="color-green font-w7 label-required-prev">目的地仓库</span>
                <select name="receiver_code" class="layui-select" required lay-search>
                    <option value="">请选择</option>
                    {foreach $warehouses as $k=>$v}
                    {if isset($vo.receiver_code) and $vo.receiver_code eq $v.ware_code}
                    <option selected value="{$v.ware_code}">{$v.ware_name}</option>
                    {else}
                    <option  value="{$v.ware_code}">{$v.ware_name}</option>
                    {/if}
                    {/foreach}
                </select>
            </div>
            {/notempty}

            <label class="layui-form-item relative block">
                <span class="color-green font-w7">车牌号</span>
                <input class="layui-input" name="car_number" type="text" placeholder="请输入" 
                    value='{$vo.car_number|default=""}'>
            </label>
            <label class="layui-form-item relative block layui-col-xs4">
                <span class="color-green font-w7">发货日期</span>
                <input data-date-input name="create_date" value="{$vo.create_date|default=''}" placeholder="{:lang('请选择创建时间')}"
                    class="layui-input">
            </label>


            <label class="layui-form-item relative block">
                <span class="color-green font-w7">备注</span>
                <textarea class="layui-textarea" name="remark" placeholder="请输入"
                    >{$vo.remark|default=''}</textarea>
            </label>
        </div>

        <div class="layui-col-xs8">
            <div class="grid-demo" style="margin-left: 20px;">
                

                <table class="layui-table">
                    <colgroup>
                        <col width="100">
                        <col width="100">
                        <col width="100">
                        <col width="150">
                        <col width="100">
                        <col width="150">
                    </colgroup>
                    <thead>
                        <tr>
                            <th>等级</th>
                            <th>重量(斤)</th>
                            <th>件数</th>
                            <th>散件重量</th>
                            <th>总重</th>
                            <th>价格(¥)</th>
                            <th>金额(¥)</th>
                           
                        </tr>
                    </thead>
                    <tbody>

                        <tr ng-repeat="rows in levelsAll track by $index">
                            <td>
                                {{rows.level_name}}
                            </td>
                            <td>
                                <input class="layui-input" type="number" ng-change="calculate($index)"  ng-model="rows.weight" placeholder="重量" />
                            </td>
                            <td>
                                <input class="layui-input" type="number" ng-change="calculate($index)" ng-model="rows.num"  placeholder="件数" />
                            </td>
                            <td>
                                <input class="layui-input" type="number" ng-change="calculate($index)" ng-model="rows.other_weight" placeholder="重量" />
                            </td>
                            <td>
                                <input class="layui-input" type="number" disabled ng-model="rows.gross_weight"   placeholder="总重" />
                            </td>
                           
                            <td>
                                <input class="layui-input" type="number" ng-change="calculate($index)" ng-model="rows.unit_price" placeholder="价格" />
                            </td>
                            <td>
                                <input class="layui-input" type="number"  disabled ng-model="rows.price" placeholder="金额" />
                            </td>
                        </tr>
                      
                    </tbody>
                </table>

                
            </div>
        </div>
    


     

        <div class="hr-line-dashed margin-top-40"></div>
        <label class="layui-hide">
            <textarea name="details">{{levelsAll}}</textarea>
        </label>
        {notempty name='vo.id'}<input name='id' type='hidden' value='{$vo.id}'>{/notempty}
        {notempty name='vo.bill_no'}<input name='bill_no' type='hidden' value='{$vo.bill_no}'>{/notempty}

        <div class="layui-form-item text-center">
            <button class="layui-btn layui-btn-danger" ng-click="pageBack()" type="button">取消编辑</button>
            <button class="layui-btn" type="submit">保存商品</button>
        </div>
    </div>
</form>
{/block}

{block name='script'}
<label class="layui-hide">
    {if isset($details) && $details != '' }
    <textarea id="levels">{$details|raw|default=''}</textarea>
    {else}
    <textarea id="levels">{$levels|raw|default=''}</textarea>
    {/if}
</label>

<script>
 

    /*! 加载扩展插件 */
    require([ 'angular'], function () {
        var app = angular.module("GoodsForm", []).run(callback);
        angular.bootstrap(document.getElementById(app.name), [app.name]);

   
        function callback($rootScope) {
           
            $rootScope.levels = Object.values(angular.fromJson(angular.element('#levels').val() || '[]') || []);
             // 初始化分类
             $rootScope.initLevels = function() {
                    $rootScope.levelsAll = [];
                    console.log($rootScope.levels)
                    for(let i=0; i<$rootScope.levels.length; i++) {
                        $rootScope.levelsAll.push({
                            level_name: $rootScope.levels[i].name || $rootScope.levels[i].level_name,
                            level_code: $rootScope.levels[i].code || $rootScope.levels[i].level_code,
                            weight: $rootScope.levels[i].weight || 0,
                            num: $rootScope.levels[i].num || 0,
                            other_weight: $rootScope.levels[i].other_weight || 0,
                            gross_weight: $rootScope.levels[i].gross_weight || 0,
                            unit_price: parseFloat($rootScope.levels[i].unit_price) || 0.00,
                            price: parseFloat($rootScope.levels[i].price) || 0.00
                        });
                    }
                };
                $rootScope.initLevels();
            
            $rootScope.pageBack = function () {
                $.msg.confirm('确定要取消编辑吗？', function (index) {
                    history.back(), $.msg.close(index);
                });
            };
            
                  // 计算单级数据
                  $rootScope.calculate = function(index) {
                    const level = $rootScope.levelsAll[index];
                    // 计算总重量（件数*420 + 散件）
                    level.gross_weight = level.weight * level.num + level.other_weight;
                    
                    level.price = level.gross_weight * level.unit_price;
                    
                    // 更新总金额
                    //$rootScope.updateTotal();
                };
           
        }
    });

</script>
{/block}